/*
 * TOP SECRET
 * Copyright 2006-2015 Transsion.com All right reserved. This software is the
 * confidential and proprietary information of Transsion.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.ng;

import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.common.http.HttpClientUtil;
import com.yunji.framework_template.common.util.StringUtil;
import com.yunji.framework_template.common.web.HttpResult;
import lombok.extern.slf4j.Slf4j;


/** 
 * ClassName:LindaikejisblogImageHandler <br/> 
 * Date:     2018年12月17日 下午7:59:28 <br/> 
 * @author   fenglibin1982@163.com
 * @Blog     http://blog.csdn.net/fenglibing
 * @version  
 * @see       
 */
@Service
@Slf4j
public class LindaikejisblogImageHandler extends ContentImageHandler {

    @Override
    public String getImage(String contentUrl) {
        try {
            HttpResult result = HttpClientUtil.doGet(contentUrl);
            if (result == null || result.getCode() != 200 || StringUtil.isEmpty(result.getContent())) {
                return null;
            }
            Element contentElement = new Element(Tag.valueOf("contents"), contentUrl);
            contentElement.html(result.getContent());
            Element featuredImageElement = contentElement.select("div[class=\"col-md-12\"]>article>summary>p>img").first();
            if (featuredImageElement != null) {
                return featuredImageElement.absUrl("src");
            }
        } catch (Exception e) {
            log.error("Get image from url " + contentUrl + " exception happened:" + e.getMessage(), e);
        }
        return null;
    }
}
